﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using bwtSocket;
using DynamicExpresso;
using CosAging;
using CosAging.Windows;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Asn1.Crmf;
using Org.BouncyCastle.Crmf;
using static CosAging.JHCLIB;
using static CosAging.AgeMotion;
using static CosAging.PublicClass;
using Timer = System.Timers.Timer;
using static CosAging.iniFileWR;
using Google.Protobuf.WellKnownTypes;
using Remotion.Linq.Collections;
using System.Runtime.InteropServices;
using CosAging.Class;
using SqlSugar.Extensions;
using Newtonsoft.Json.Linq;
using SqlSugar;
using MySqlX.XDevAPI.Common;
using Microsoft.Expression.Shapes;
using System.Windows.Forms;
using MessageBox = System.Windows.MessageBox;
using UserControl = System.Windows.Controls.UserControl;
using TextBox = System.Windows.Controls.TextBox;
using System.Data.SqlClient;
using System.Data;
using System.Data.SQLite;
using System.IO.Ports;
using System.Collections;
using System.Net.Sockets;
using System.Reflection;
using MySqlX.XDevAPI.Relational;
using System.IO;
using System.Net.NetworkInformation;
using System.CodeDom;
using NPOI.SS.Util;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Diagnostics.Eventing.Reader;
using System.Globalization;
using NPOI.HSSF.Record;
using static System.Net.Mime.MediaTypeNames;
using System.Diagnostics;
using Microsoft.Win32;

namespace CosAging.View
{

    public class CBlist
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Value { get; set; }
    }
    public class BoundList
    {
        public double Upper { get; set; }
        public double Lower { get; set; }
    }

    /// <summary>
    /// SystemMonitor.xaml 的交互逻辑
    /// </summary>
    public partial class SystemMonitor2 : UserControl
    {

        //public JHCLIBBinding JBD = new JHCLIBBinding();
        public Dictionary<string, BoundList> DCBL = new Dictionary<string, BoundList>();
        //string[] strName = { "步进距离", "步进速度", "初始位置", "断续步进次数", "垂直距离", "标刻速度", "跳转速度", "红光速度", "Offset" };
        //double[] Upper = { 100, 10, 100, 1000, 200, 2000, 2000, 2000, 30 };
        //double[] Lower = { -100, 0, -100, 1, 100, 1, 1, 1, -30 };
        public Timer SendCommandTimer = new Timer();
        public Timer AddQueryTimer = new Timer();
        public Timer RecondTimer = new Timer();
        public Timer AgingCountdown = new Timer();
        public Timer 断线检测 = new Timer();
        public CBlist CBlist = new CBlist();
        public List<workingdata> Listworkingdata = new List<workingdata>();
        public List<operationrecords> Listoperationrecords = new List<operationrecords>();
        //DataTable Listworkingdata=new DataTable();
        public static SerialPort serialPort;
        public static LinkedList<byte[]> commandList = new LinkedList<byte[]>();
        public static LinkedList<byte[]> QueryList = new LinkedList<byte[]>();
        public DataView dv;
        private static bool datareceived = true, cmdsending = false, Querycmdadding = false, Sendcmdadding = false;
        private static bool 手动开始自锁 = false, 手动结束自锁 = false, 数据同步自锁 = false, 数据查询自锁 = false, 气缸操作自锁 = false, 编号操作自锁 = false;
        private static int currentminute = 0;//整分钟记录数据
        private static string sendsort = "";
        private int AddQueryTimerInterval = 1000;
        private byte[] 待处理数组 = new byte[0];
        private static string status="";//阶梯加电状态
        private static double currentStage1=0.0;//第一阶段电流值
        private static string positioning = "";//摆放出错的位置信息

        public SystemMonitor2()
        {
            //PublicClass.计时起点();
            InitializeComponent();
            CreatObject();
            this.DataContext = CosBobject;
            Class_Initialize();
            
            串口设置();
            
            加载数据库();
            TimerSetting();
            GetCurrentStage1();
            //结尾动作();
            //ProgressBegin();
            //PublicClass.计时终点();
            //PublicClass.Logger.Info(PublicClass.时间差);

        }
        private void Class_Initialize()
        {
            try
            {
                //checkbox通道选择绑定
                foreach (UIElement CB in MainGrid.Children)
                {
                    if (CB is System.Windows.Controls.CheckBox)
                    {
                        System.Windows.Controls.CheckBox wantedLabel = (System.Windows.Controls.CheckBox)CB;
                        int Index = Convert.ToInt32(wantedLabel.Tag);
                        wantedLabel.DataContext = PublicClass.CosBobject.CD测试单元格[Index - 1];
                    }
                }
                for (int i = 0; i < CosBobject.CD测试单元格.Count; i++)
                {
                    监控数据(CosBobject.CD测试单元格[i].通道号);
                }
                通道1状态.DataContext = PublicClass.CosBobject.CD测试单元格[0];
                通道2状态.DataContext = PublicClass.CosBobject.CD测试单元格[1];
                通道3状态.DataContext = PublicClass.CosBobject.CD测试单元格[2];
                通道4状态.DataContext = PublicClass.CosBobject.CD测试单元格[3];
                通道5状态.DataContext = PublicClass.CosBobject.CD测试单元格[4];
                通道6状态.DataContext = PublicClass.CosBobject.CD测试单元格[5];
                通道7状态.DataContext = PublicClass.CosBobject.CD测试单元格[6];
                通道8状态.DataContext = PublicClass.CosBobject.CD测试单元格[7];
            }
            catch (Exception ex)
            {
                NLOGex(ex);
            }
        }
        private void 串口设置()
        {
            try
            {
                myIniFile.iniFileName = Apppath + INISYS;
                string portName = myIniFile.ReadString("comm", "串口号", 99);
                int baudRate = Convert.ToInt32(myIniFile.ReadString("comm", "波特率", 99));
                Parity parity = (Parity)Convert.ToInt32(myIniFile.ReadString("comm", "校验位", 99));
                int dataBits = Convert.ToInt32(myIniFile.ReadString("comm", "数据位", 99));
                StopBits stopBits = (StopBits)Convert.ToInt32(myIniFile.ReadString("comm", "停止位", 99));
                AddQueryTimerInterval = Convert.ToInt32(myIniFile.ReadString("自定义参数", "AddQueryTimerInterval", 99));

                // 初始化串口
                serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
                serialPort.ReadBufferSize = 4096;
                serialPort.WriteBufferSize = 4096;
                serialPort.DtrEnable = true;
                serialPort.RtsEnable = true;
                serialPort.DataReceived += SerialPort_DataReceived;

                //if (serialPort != null && serialPort.IsOpen)
                //{
                //    serialPort.Close();
                //}
            }
            catch (Exception ex)
            {
                NLOGex(ex);
            }
        }
        private void TimerSetting()
        {

            SendCommandTimer.AutoReset = false;
            SendCommandTimer.Interval = 10;
            SendCommandTimer.Enabled = true;
            SendCommandTimer.Elapsed += SendCommandTimerEvent;

            AddQueryTimer.AutoReset = true;
            AddQueryTimer.Interval = AddQueryTimerInterval;
            AddQueryTimer.Enabled = false;
            AddQueryTimer.Elapsed += AddQueryTimerEvent;

            RecondTimer.AutoReset = false;
            RecondTimer.Interval = 10;
            RecondTimer.Enabled = true;
            RecondTimer.Elapsed += RecondTimerEvent;

            AgingCountdown.AutoReset = false;
            AgingCountdown.Interval = 5000;
            AgingCountdown.Enabled = true;
            AgingCountdown.Elapsed += AgingCountdownEvent;

            断线检测.AutoReset = false;
            断线检测.Interval = 1000;
            断线检测.Enabled = true;
            断线检测.Elapsed += 断线检测Event;
        }
        private void 加载数据库()
        {
            var connectionString = @"Data Source=" + Apppath + "datatable.db";
            var connection = new SQLiteConnection(connectionString);
            connection.Open();
            string SheetName = "workingdata";
            var query = "select * from " + SheetName + " where IsCompleted='未完成'  ";
            using (SQLiteCommand command = new SQLiteCommand(query, connection))
            {
                DataTable dt = new DataTable();
                SQLiteDataReader SQLitereader = command.ExecuteReader();
                dt.Load(SQLitereader);

                DataRow[] DataRow = dt.Select();
                Listworkingdata.Clear();
                for (int i = 0; i < DataRow.Count(); i++)
                {
                    int 通道号 = Convert.ToInt32(DataRow[i]["Agingunit"]);

                    CosBobject.CD测试单元格[通道号 - 1].物料编码 = Convert.ToString(DataRow[i]["Materialcode"]);
                    CosBobject.CD测试单元格[通道号 - 1].产品批号 = Convert.ToString(DataRow[i]["Batchnumber"]);
                    CosBobject.CD测试单元格[通道号 - 1].产品型号 = Convert.ToString(DataRow[i]["Model"]);
                    CosBobject.CD测试单元格[通道号 - 1].开始时间 = Convert.ToString(DataRow[i]["Begintime"]);
                    CosBobject.CD测试单元格[通道号 - 1].结束时间 = Convert.ToString(DataRow[i]["Endtime"]);

                    workingdata WD = new workingdata()
                    {
                        batchnumber = Convert.ToString(DataRow[i]["Batchnumber"]),
                        model = Convert.ToString(DataRow[i]["Model"]),
                        agingcurrent = Convert.ToString(DataRow[i]["Agingcurrent"]),
                        agingtime = Convert.ToString(DataRow[i]["Agingtime"]),
                        agingunit = Convert.ToString(DataRow[i]["Agingunit"]),
                        begintime = Convert.ToString(DataRow[i]["Begintime"]),
                        endtime = Convert.ToString(DataRow[i]["Endtime"]),
                        stoptime = Convert.ToString(DataRow[i]["Stoptime"])
                    };
                    Listworkingdata.Add(WD);
                }

                App.Current.Dispatcher.Invoke((Action)(() =>
                {
                    正在加工数据.ItemsSource = null;
                    正在加工数据.Items.Clear();
                    正在加工数据.ItemsSource = Listworkingdata;
                }));
            }

            SheetName = "operationrecords";

            query = "SELECT * FROM  " + SheetName + "  ORDER BY ID DESC LIMIT 100";
            using (SQLiteCommand command = new SQLiteCommand(query, connection))
            {
                DataTable dt = new DataTable();
                SQLiteDataReader SQLitereader = command.ExecuteReader();
                dt.Load(SQLitereader);

                string expression = "ID>0";
                string sortOrder = "ID DESC";
                DataRow[] DataRow = dt.Select(expression, sortOrder);


                Listoperationrecords.Clear();
                for (int i = 0; i < DataRow.Count(); i++)
                {
                    operationrecords OR = new operationrecords()
                    {
                        operatetime = Convert.ToString(DataRow[i]["operatetime"]),
                        operatecontent = Convert.ToString(DataRow[i]["operatecontent"])
                    };
                    Listoperationrecords.Add(OR);
                }

                App.Current.Dispatcher.Invoke((Action)(() =>
                {
                    操作记录.ItemsSource = null;
                    操作记录.Items.Clear();
                    操作记录.ItemsSource = Listoperationrecords;
                    操作记录.ScrollIntoView(操作记录.Items[操作记录.Items.Count - 1]);
                }));
            }
        }
        private void 结尾动作()
        {
            try
            {
                for (int i = 0; i < 8; i++)
                {
                    workingdata WD = new workingdata()
                    {
                        batchnumber = i.ToString(),
                        model = i.ToString(),
                        agingcurrent = i.ToString(),
                        agingtime = i.ToString(),
                        agingunit = i.ToString(),
                        begintime = i.ToString(),
                        endtime = i.ToString()
                    };
                    Listworkingdata.Add(WD);
                }
                App.Current.Dispatcher.Invoke((Action)(() =>
                {
                    正在加工数据.ItemsSource = null;
                    正在加工数据.Items.Clear();
                    正在加工数据.ItemsSource = Listworkingdata;
                }));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void SendCommandTimerEvent(object sender, EventArgs e)
        {
            try
            {
                while (MainWindow.Systemrunning)
                {
                    if (cmdsending == false)
                    {
                        cmdsending = true;
                        if (!serialPort.IsOpen)
                        {
                            string[] portNames = SerialPort.GetPortNames();
                            foreach (string portName in portNames)
                            {
                                if (portName == serialPort.PortName)
                                {
                                    Thread.Sleep(500);
                                    serialPort.Open();
                                    break;
                                }
                            }
                        }
                        else
                        {
                            //优先发命令
                            if (commandList.Count > 0)
                            {
                                if (Sendcmdadding == false)
                                {
                                    Thread.Sleep(50);
                                    byte[] sendbyte = commandList.ElementAt(0);
                                    待处理数组 = new byte[0];
                                    serialPort.Write(sendbyte, 0, sendbyte.Length);
                                    int 通道号 = commandList.ElementAt(0)[1];
                                    CosBobject.CD测试单元格[通道号 - 1].DT发送时间 = DateTime.Now;
                                    commandList.RemoveFirst();
                                }
                            }
                            else
                            {
                                if (Sendcmdadding == false)
                                {
                                    for (int i = 0; i < QueryList.Count; i++)
                                    {
                                        Thread.Sleep(50);
                                        byte[] sendbyte = QueryList.ElementAt(i);
                                        待处理数组 = new byte[0];
                                        serialPort.Write(sendbyte, 0, sendbyte.Length);
                                        int 通道号 = QueryList.ElementAt(i)[1];
                                        CosBobject.CD测试单元格[通道号 - 1].DT发送时间 = DateTime.Now;
                                    }
                                }
                            }
                        }
                        cmdsending = false;
                    }
                }
            }
            catch (Exception ex)
            {
                NLOGex(ex); cmdsending = false; SendCommandTimer.Enabled = true;
            }
        }
        private void AddQueryTimerEvent(object sender, EventArgs e)
        {
            try
            {
                if (QueryList.Count == 0)
                {
                    Querycmdadding = true;
                    for (int i = 0; i < CosBobject.CD测试单元格.Count; i++)
                    {
                        监控数据(CosBobject.CD测试单元格[i].通道号);
                    }
                    Querycmdadding = false;
                }
            }
            catch (Exception ex)
            {
                NLOGex(ex);
            }
        }
        private void RecondTimerEvent(object sender, EventArgs e)
        {
            try
            {
                while (MainWindow.Systemrunning)
                {
                    if (DateTime.Now.Second % 10 == 0 & DateTime.Now.Second != currentminute)
                    {
                        currentminute = DateTime.Now.Second;
                        for (int i = 0; i < CosBobject.CD测试单元格.Count; i++)
                        {
                            if (CosBobject.CD测试单元格[i].通道状态 > 0 & CosBobject.CD测试单元格[i].通道状态 < 4 & CosBobject.CD测试单元格[i].产品批号 != "" & CosBobject.CD测试单元格[i].产品批号 != "N/A")
                            {

                                string format = "yyyy/MM/dd HH:mm:ss";
                                DateTime result;
                                if (DateTime.TryParseExact(CosBobject.CD测试单元格[i].开始时间, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
                                {
                                    测试单元格 dd = CosBobject.CD测试单元格[i];
                                    string DATENOW = result.ToString("yyyyMMddHHmmss");
                                    string 批次号 = CosBobject.CD测试单元格[i].产品批号;
                                    string 通道号 = CosBobject.CD测试单元格[i].通道号.ToString();

                                    string dic = Directory.GetCurrentDirectory() + "\\" + "老化过程记录" + "\\" + 批次号;
                                    //string dic = Apppath + "\\" + "老化过程记录" + "\\" + DATENOW;
                                    if (!Directory.Exists(dic))
                                    {
                                        Directory.CreateDirectory(dic);
                                    }
                                    string filename = dic + "\\" + DATENOW + "-" + 批次号 + "-" + 通道号 + ".txt";

                                    FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
                                    string strsend = DateTime.Now + "," + CosBobject.CD测试单元格[i].电流;  // ","+ CosBobject.CD测试单元格[i].CD测试芯片[0].double温度
                                    for (int j = 0; j < 8; j++)
                                    {
                                        strsend = strsend + "," + CosBobject.CD测试单元格[i].CD测试芯片[j].double温度;
                                    }
                                    for (int j = 0; j < 30; j++)
                                    {
                                        strsend = strsend + "," + CosBobject.CD测试单元格[i].CD测试芯片[j].double功率;
                                    }
                                    strsend = strsend + "," + CosBobject.CD测试单元格[i].故障代码;
                                    using (StreamWriter sw = new StreamWriter(fs))
                                    {
                                        sw.WriteLine(strsend);
                                    }
                                    fs.Close();
                                }
                                else
                                {
                                    Console.WriteLine("转换失败");
                                    return;
                                }
                            }
                        }
                    }
                    Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                NLOGex(ex);
            }
        }
        private void AgingCountdownEvent(object sender, EventArgs e)
        {
            try
            {
                while (MainWindow.Systemrunning)
                {
                    if (Sendcmdadding == false)
                    {
                        Sendcmdadding = true;
                        string STR产品批号 = "", STR结束时间 = "";
                        for (int i = 0; i < CosBobject.CD测试单元格.Count; i++)
                        {
                            Thread.Sleep(10);
                            string str = CosBobject.CD测试单元格[i].结束时间;
                            if (str != "自动完成" & str != "手动中止" & str != "异常停止" & str != "N/A")
                            {
                                string d1 = CosBobject.CD测试单元格[i].结束时间;
                                string d2 = CosBobject.CD测试单元格[i].开始时间;
                                DateTime t1 = DateTime.Now; // 第一个时间点
                                DateTime t2 = DateTime.ParseExact(CosBobject.CD测试单元格[i].结束时间, "yyyy/MM/dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);  // 第二个时间点（比第一个时间点晚30秒）
                                DateTime t3 = DateTime.ParseExact(CosBobject.CD测试单元格[i].开始时间, "yyyy/MM/dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);  // 第二个时间点（比第一个时间点晚30秒）

                                // 计算两个时间之间的秒数差值
                                TimeSpan diff1 = t1 - t2; TimeSpan diff2 = t1 - t3;
                                int seconds1 = (int)diff1.TotalSeconds; int seconds2 = (int)diff2.TotalSeconds;
                                if (seconds1 >= 0 & CosBobject.CD测试单元格[i].通道状态 == 1)
                                {
                                    STR产品批号 = CosBobject.CD测试单元格[i].产品批号;
                                    STR结束时间 = CosBobject.CD测试单元格[i].结束时间;
                                    //第一步还原通道参数
                                    CosBobject.CD测试单元格[i].物料编码 = "";
                                    CosBobject.CD测试单元格[i].产品批号 = "";
                                    CosBobject.CD测试单元格[i].开始时间 = "N/A";
                                    CosBobject.CD测试单元格[i].结束时间 = "自动完成";
                                    //第二步发命令给下位机停止老化

                                    结束流程(CosBobject.CD测试单元格[i].通道号);
                                    添加操作记录(CosBobject.CD测试单元格[i].通道号, "自动完成");
                                    if (STR产品批号 != "" & STR结束时间 != "")
                                    {
                                        string stoptime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                                        更新老化记录(STR产品批号, STR结束时间, i + 1, stoptime);
                                        更新老化记录显示();
                                    }
                                }
                                else if (seconds2 >= 15)
                                {
                                    if (CosBobject.CD测试单元格[i].通道状态 == 0 || CosBobject.CD测试单元格[i].通道状态 == 2)
                                    {
                                        STR产品批号 = CosBobject.CD测试单元格[i].产品批号;
                                        STR结束时间 = CosBobject.CD测试单元格[i].结束时间;
                                        //第一步还原通道参数
                                        CosBobject.CD测试单元格[i].物料编码 = "";
                                        CosBobject.CD测试单元格[i].产品批号 = "";
                                        CosBobject.CD测试单元格[i].开始时间 = "N/A";
                                        CosBobject.CD测试单元格[i].结束时间 = "异常停止";
                                        //第二步发命令给下位机停止老化
                                        结束流程(CosBobject.CD测试单元格[i].通道号);
                                        添加操作记录(CosBobject.CD测试单元格[i].通道号, "异常停止:");
                                        if (STR产品批号 != "" & STR结束时间 != "")
                                        {
                                            string stoptime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                                            更新老化记录(STR产品批号, STR结束时间, i + 1, stoptime);
                                            更新老化记录显示();
                                        }
                                    }
                                    else if(CosBobject.CD测试单元格[i].通道状态 == 5)
                                    {
                                        STR产品批号 = CosBobject.CD测试单元格[i].产品批号;
                                        STR结束时间 = CosBobject.CD测试单元格[i].结束时间;
                                        //第一步还原通道参数
                                        CosBobject.CD测试单元格[i].物料编码 = "";
                                        CosBobject.CD测试单元格[i].产品批号 = "";
                                        CosBobject.CD测试单元格[i].开始时间 = "N/A";
                                        CosBobject.CD测试单元格[i].结束时间 = "异常停止";
                                        //第二步发命令给下位机停止老化
                                        结束流程(CosBobject.CD测试单元格[i].通道号);
                                        添加操作记录(CosBobject.CD测试单元格[i].通道号, positioning);
                                        if (STR产品批号 != "" & STR结束时间 != "")
                                        {
                                            string stoptime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                                            更新老化记录(STR产品批号, STR结束时间, i + 1, stoptime);
                                            更新老化记录显示();
                                        }
                                        App.Current.Dispatcher.Invoke((Action)(() =>
                                        {
                                            MessageBox.Show(CosBobject.CD测试单元格[i].通道号.ToString()+ positioning);
                                        }));
                                    }
                                }
                            }
                        }
                        Sendcmdadding = false;
                    }
                    Thread.Sleep(1000);
                }
            }
            catch (Exception ex)
            {
                PublicClass.NLOGex(ex); Sendcmdadding = false; AgingCountdown.Enabled = true;
            }
        }
        private void 断线检测Event(object sender, EventArgs e)
        {
            try
            {
                while (MainWindow.Systemrunning)
                {
                    if (serialPort.IsOpen)
                    {
                        for (int i = 0; i < CosBobject.CD测试单元格.Count(); i++)
                        {
                            Thread.Sleep(10);
                            DateTime t1 = DateTime.Now; // 第一个时间点
                            DateTime t2 = CosBobject.CD测试单元格[i].DT发送时间;
                            DateTime t3 = CosBobject.CD测试单元格[i].DT收到时间;

                            // 计算两个时间之间的秒数差值
                            TimeSpan diff1 = t1 - t2; TimeSpan diff2 = t2 - t3;
                            int seconds1 = (int)diff1.TotalSeconds; int seconds2 = (int)diff2.TotalSeconds;
                            if (seconds2 > 1)
                            {
                                //PublicClass.Logger.Error("通道号:" + CosBobject.CD测试单元格[i].通道号.ToString() + "通道通道状态:" + CosBobject.CD测试单元格[i].通道状态.ToString());
                                //PublicClass.Logger.Error("发送时间:" + CosBobject.CD测试单元格[i].DT发送时间.ToString() + "收到时间:" + CosBobject.CD测试单元格[i].DT收到时间.ToString());
                                CosBobject.CD测试单元格[i].通道状态 = 4;
                                for (int j = 0; j < 8; j++)
                                {
                                    CosBobject.CD测试单元格[i].CD测试芯片[j].double温度 = 0;
                                }
                                for (int k = 0; k < 30; k++)
                                {
                                    CosBobject.CD测试单元格[i].CD测试芯片[k].double功率 = 0;
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < CosBobject.CD测试单元格.Count(); i++)
                        {
                            CosBobject.CD测试单元格[i].通道状态 = 4;
                            for (int j = 0; j < 8; j++)
                            {
                                CosBobject.CD测试单元格[i].CD测试芯片[j].double温度 = 0;
                            }
                            for (int k = 0; k < 30; k++)
                            {
                                CosBobject.CD测试单元格[i].CD测试芯片[k].double功率 = 0;
                            }
                        }

                    }
                    Thread.Sleep(10);
                }
            }
            catch (Exception ex)
            {
                NLOGex(ex); 断线检测.Enabled = true;
            }
        }
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                //string receivedData = serialPort.ReadExisting();
                int reLen = serialPort.BytesToRead; //已接收，在接收缓冲区中等待计算机接收的字符数
                if (reLen > 0)
                {
                    byte[] receivedData = new byte[reLen];
                    serialPort.Read(receivedData, 0, reLen);
                    //serialPort.ReadExisting
                    if (待处理数组.Count() == 0)
                    {
                        待处理数组 = receivedData;
                    }
                    else
                    {
                        待处理数组 = 待处理数组.Concat(receivedData).Where(a => (int)a >= 0).ToArray();
                        //待处理数组 = byte数组合并(待处理数组, receivedData);
                    }

                    byte[] 需校验数组 = new byte[待处理数组.Count() - 1];
                    Array.ConstrainedCopy(待处理数组, 0, 需校验数组, 0, 待处理数组.Count() - 1);
                    byte[] 校验结果 = PublicClass.CS校验和(需校验数组.ToArray());
                    bool areEqual = 待处理数组.SequenceEqual(校验结果);
                    if (areEqual)
                    {
                        int 通道号 = 待处理数组[1];
                        CosBobject.CD测试单元格[通道号 - 1].DT收到时间 = DateTime.Now;
                        //PublicClass.Logger.Error("通道号:" + 通道号.ToString() + "收到时间:" + CosBobject.CD测试单元格[通道号 - 1].DT收到时间.ToString());
                        if (待处理数组[0] == 0x38)//取通确认
                        {

                            if (待处理数组[2] == 0x01)//取通成功
                            {

                            }
                            else if (待处理数组[2] == 0x1B)//ID操作成功
                            {
                                App.Current.Dispatcher.Invoke((Action)(() =>
                                {
                                    MessageBox.Show("ID设置成功");
                                }));
                            }
                            else if (待处理数组[2] == 0x13)//查询
                            {
                                CosBobject.CD测试单元格[通道号 - 1].double电流 = (待处理数组[3] * 255 + 待处理数组[4]) / 10.0;
                                //如果之前是断开状态，要恢复状态
                                //if (CosBobject.CD测试单元格[通道号 - 1].double电流 > 0 & CosBobject.CD测试单元格[通道号 - 1].通道状态 ==4)
                                //{
                                //    CosBobject.CD测试单元格[通道号 - 1].通道状态 = 1;
                                //}
                                //else
                                //{
                                //    CosBobject.CD测试单元格[通道号 - 1].通道状态 = 0;
                                //}

                                for (int i = 0; i < 8; i++)
                                {
                                    CosBobject.CD测试单元格[通道号 - 1].CD测试芯片[i].double温度 = 待处理数组[i + 5] / 1.0;

                                    if (CosBobject.CD测试单元格[通道号 - 1].CD测试芯片[i].double温度 > CosBobject.CD测试单元格[通道号 - 1].单元报警温度)
                                    {
                                        //结束流程(PublicClass.CosBobject.CD测试单元格[通道号 - 1].通道号);
                                        添加操作记录(CosBobject.CD测试单元格[i].通道号, "通道" + 通道号 + "温度过高" + CosBobject.CD测试单元格[通道号 - 1].CD测试芯片[i].double温度);
                                        //string stoptime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                                        //更新老化记录(CosBobject.CD测试单元格[通道号 - 1].产品批号, CosBobject.CD测试单元格[通道号 - 1].结束时间, i + 1, stoptime);                                     
                                        //CosBobject.CD测试单元格[通道号 - 1].单元报警温度 = 999;
                                    }
                                }
                                for (int i = 0; i < 30; i++)
                                {
                                    CosBobject.CD测试单元格[通道号 - 1].CD测试芯片[i].double功率 = (待处理数组[i * 2 + 13] * 255 + 待处理数组[i * 2 + 14]);
                                    if (CosBobject.CD测试单元格[通道号 - 1].double电流 > 0 && CosBobject.CD测试单元格[通道号 - 1].double电流 <= currentStage1)
                                    {
                                        if (CosBobject.CD测试单元格[通道号 - 1].CD测试芯片[i].double功率 == 0 && 通道号!=2)
                                        {
                                            //CosBobject.CD测试单元格[通道号 - 1].通道状态 = 5;
                                            positioning = "芯片" + i + "需检查放置是否到位";
                                        }
                                        
                                    }
                                }
                                CosBobject.CD测试单元格[通道号 - 1].故障代码 = 待处理数组[73];

                                if (CosBobject.CD测试单元格[通道号 - 1].故障代码 == 0)
                                {
                                    if (CosBobject.CD测试单元格[通道号 - 1].double电流 > 0)
                                    {
                                        CosBobject.CD测试单元格[通道号 - 1].通道状态 = 1;
                                    }
                                    else
                                    {
                                        CosBobject.CD测试单元格[通道号 - 1].通道状态 = 0;
                                        //添加操作记录(通道号, "电流为0");
                                    }
                                }
                                else
                                {
                                    int failure = CosBobject.CD测试单元格[通道号 - 1].故障代码;
                                    string abnormal = PublicClass.故障代码对应表[failure];
                                    添加操作记录(通道号, abnormal);
                                    CosBobject.CD测试单元格[通道号 - 1].通道状态 = 2;
                                }
                                

                            }
                            else if (待处理数组[2] == 0x12)//结束老化成功
                            {
                                CosBobject.CD测试单元格[通道号 - 1].通道状态 = 0;
                                添加操作记录(通道号, "结束老化成功");
                                //Console.Write("结束老化成功");
                            }
                            else if (待处理数组[2] == 0x22)//结束老化失败
                            {
                                添加操作记录(通道号, "结束老化失败");
                                //Console.Write("结束老化失败");
                            }
                            else if (待处理数组[2] == 0x11)//设置加电成功
                            {
                                CosBobject.CD测试单元格[通道号 - 1].通道状态 = 1;
                                新增老化记录(通道号);
                                更新老化记录显示();
                                //解除选中
                                for (int i = 0; i < CosBobject.CD测试单元格.Count; i++)
                                {
                                    CosBobject.CD测试单元格[i].是否被选中 = false;
                                }
                                添加操作记录(通道号, "加电成功");
                            }
                            else if (待处理数组[2] == 0x21)//设置加电失败
                            {
                                添加操作记录(通道号, "加电失败");
                            }
                            else if (待处理数组[2] == 0x1D)//设置加电失败
                            {
                                添加操作记录(通道号, "阶梯加电参数设置失败");
                            }
                            else if (待处理数组[2] == 0x1C)//设置加电成功
                            {
                                添加操作记录(通道号, "阶梯加电参数设置成功");
                            }
                            else if (待处理数组[2] == 0x19)//脉冲设置成功
                            {

                            }
                            else if (待处理数组[2] == 0x14)//电磁锁设置成功
                            {

                            }
                            else if (待处理数组[2] == 0x15)//传感器到位
                            {

                            }
                            else if (待处理数组[2] == 0x25)//传感器没到位
                            {

                            }
                            else if (待处理数组[2] == 0x1A)//气缸操作成功
                            {

                            }

                            //datareceived = true;
                            待处理数组 = new byte[0];
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        //string strrev = Encoding.UTF8.GetString(待处理数组);
                        //Logger.Error(strrev);
                        return;
                    }
                    //Console.WriteLine("收到数据: " + receivedData);
                }
            }
            catch (Exception ex)
            {
                NLOGex(ex);
            }
        }
        private void 数据同步(object sender, RoutedEventArgs e)
        {
            if (数据同步自锁 == false)
            {
                MessageBoxResult result = MessageBox.Show("同步过程大约为15秒，且需要连接公司网络，请问是否继续?", "确认是否要同步数据", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    数据同步自锁 = true;
                    if (SQL.SQLitelhspec())
                    {
                        MessageBox.Show("数据同步完成！");
                    }
                    else
                    {
                        MessageBox.Show("数据同步失败！");
                    }
                    数据同步自锁 = false;
                }
            }
        }
        private void 数据查询(object sender, RoutedEventArgs e)
        {
            if (数据查询自锁 == false)
            {
                数据查询自锁 = true;
                if (SQL.SQLite查询())
                {
                    MessageBox.Show("数据查询完成！");
                }
                else
                {
                    MessageBox.Show("数据查询完成！");
                }
                数据查询自锁 = false;
            }
        }
        private void 参数选择(object sender, RoutedEventArgs e)
        {
            //System.Windows.Controls.RadioButton ControlOBJECT = (System.Windows.Controls.RadioButton)sender;
            //JBD.参数号 = ControlOBJECT.Tag.ToString();
        }
        private void Enable公共(bool tf)
        {
        }
        private void PreviewTextInput_vb1(Object sender, TextCompositionEventArgs e)
        {
            System.Windows.Controls.TextBox CTextBox = (System.Windows.Controls.TextBox)sender;
            PublicClass.tb_PreviewTextInput1(CTextBox, e);
        }
        private void PreviewTextInput_vb2(Object sender, TextCompositionEventArgs e)
        {
            System.Windows.Controls.TextBox CTextBox = (System.Windows.Controls.TextBox)sender;
            PublicClass.tb_PreviewTextInput2(CTextBox, e);
        }
        private void PreviewTextInput_vb3(Object sender, TextCompositionEventArgs e)
        {
            System.Windows.Controls.TextBox CTextBox = (System.Windows.Controls.TextBox)sender;
            PublicClass.tb_PreviewTextInput3(CTextBox, e);
        }
        private void TextLostFocus(object sender, EventArgs e)
        {
            System.Windows.Controls.TextBox TextBox_Text = (System.Windows.Controls.TextBox)sender;

            bool result = DCBL.ContainsKey(TextBox_Text.Name);
            if (result)
            {
                if (TextBox_Text.Text == "")
                {
                    string strShow;
                    strShow = "参数不能为空!";
                    MessageBox.Show(strShow);
                    TextBox_Text.Text = "";
                    return;
                }
                else if (Convert.ToDouble(TextBox_Text.Text) > DCBL[TextBox_Text.Name].Upper || Convert.ToDouble(TextBox_Text.Text) < DCBL[TextBox_Text.Name].Lower)
                {
                    string strShow;
                    strShow = TextBox_Text.Name + "超限!";
                    MessageBox.Show(strShow);
                    TextBox_Text.Text = "";
                    return;
                }
            }
        }
        private void 手动开始(object sender, RoutedEventArgs e)
        {
            try
            {
                if (手动开始自锁 == false)
                {
                    手动开始自锁 = true;
                    if (老化参数完整())
                    {
                        bool 是否有可用选中项 = false;

                        for (int i = 0; i < CosBobject.CD测试单元格.Count; i++)
                        {
                            if (CosBobject.CD测试单元格[i].是否被选中 & CosBobject.CD测试单元格[i].通道状态 != 0)
                            {
                                MessageBox.Show(CosBobject.CD测试单元格[i].通道号 + "通道状态不是空闲状态，请检查后重新操作！");
                                手动开始自锁 = false;
                                return;
                            }
                            if (CosBobject.CD测试单元格[i].是否被选中 & CosBobject.CD测试单元格[i].通道状态 == 0)
                            {
                                是否有可用选中项 = true;
                            }
                        }
                        if (是否有可用选中项)
                        {
                            double TimeSpan = Convert.ToDouble(PublicClass.CosBobject.一阶段时间) * 60;
                            DateTime ENDTIME = DateTime.Now.AddMinutes(TimeSpan);
                            CosBobject.结束时间 = ENDTIME.ToString("yyyy/MM/dd HH:mm:ss");
                            Sendcmdadding = true;
                            for (int i = 0; i < CosBobject.CD测试单元格.Count; i++)
                            {
                                if (CosBobject.CD测试单元格[i].是否被选中 & CosBobject.CD测试单元格[i].通道状态 == 0)
                                {
                                    CosBobject.CD测试单元格[i].物料编码 = CosBobject.物料编码;
                                    CosBobject.CD测试单元格[i].产品批号 = CosBobject.产品批号;
                                    CosBobject.CD测试单元格[i].开始时间 = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                                    CosBobject.CD测试单元格[i].DT开始时间 = DateTime.Now;
                                    CosBobject.CD测试单元格[i].结束时间 = CosBobject.结束时间;
                                    CosBobject.CD测试单元格[i].DT结束时间 = ENDTIME;
                                    CosBobject.CD测试单元格[i].单元报警温度 = Convert.ToDouble(CosBobject.单元报警温度);
                                    设置加电状态(CosBobject.CD测试单元格[i].通道号);
                                    if (CosBobject.SelectLocation.Name == "脉冲模式")
                                    {
                                        设置脉冲(CosBobject.CD测试单元格[i].通道号);
                                    }
                                    设置参数(CosBobject.CD测试单元格[i].通道号);
                                    添加操作记录(CosBobject.CD测试单元格[i].通道号, "手动开始");
                                }
                            }
                            Sendcmdadding = false;

                        }
                        //connection.Open();
                    }
                    else
                    {
                        手动开始自锁 = false;
                        return;//参数不完整退出
                    }
                    手动开始自锁 = false;
                }
            }
            catch (Exception ex)
            {
                NLOGex(ex);
                手动开始自锁 = false;
            }
        }
        private void 气缸操作_Click(object sender, RoutedEventArgs e)
        {
            if (气缸操作自锁 == false)
            {
                气缸操作自锁 = true;
                System.Windows.Controls.Button button = (System.Windows.Controls.Button)sender;
                for (int i = 0; i < CosBobject.CD测试单元格.Count; i++)
                {
                    if (CosBobject.CD测试单元格[i].是否被选中 & CosBobject.CD测试单元格[i].通道状态 == 0)
                    {
                        设置气缸状态(CosBobject.CD测试单元格[i].通道号, button.Tag.ToString());
                    }
                }
                气缸操作自锁 = false;
            }
        }
        private void 编号设置_Click(object sender, RoutedEventArgs e)
        {
            if (编号操作自锁 == false)
            {
                编号操作自锁 = true;
                System.Windows.Controls.Button button = (System.Windows.Controls.Button)sender;
                for (int i = 0; i < CosBobject.CD测试单元格.Count; i++)
                {
                    //if (CosBobject.CD测试单元格[i].是否被选中 & CosBobject.CD测试单元格[i].通道状态 == 0)
                    //{
                    //    设置ID(CosBobject.CD测试单元格[i].通道号);
                    //}
                    if (CosBobject.CD测试单元格[i].是否被选中)
                    {
                        设置ID(CosBobject.CD测试单元格[i].通道号);
                    }
                }

                编号操作自锁 = false;
            }
        }
        public void 添加操作记录(int 通道号, string operatecontent)
        {
            try
            {
                var connectionString = @"Data Source=" + Apppath + "datatable.db";
                var connection = new SQLiteConnection(connectionString);
                connection.Open();
                string SheetName = "operationrecords";

                string operatetime = DateTime.Now.ToString();
                operatecontent = 通道号 + "号通道" + operatecontent;
                var query = "insert into " + SheetName + " (operatetime,operatecontent) VALUES ('" + operatetime + "','" + operatecontent + "')";
                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.ExecuteNonQuery();
                }
                query = "SELECT * FROM  " + SheetName + "  ORDER BY ID DESC LIMIT 100 ;";
                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    DataTable dt = new DataTable();
                    SQLiteDataReader SQLitereader = command.ExecuteReader();
                    dt.Load(SQLitereader);

                    DataRow[] DataRow = dt.Select();
                    Listoperationrecords.Clear();
                    for (int i = 0; i < DataRow.Count(); i++)
                    {
                        operationrecords OR = new operationrecords()
                        {
                            operatetime = Convert.ToString(DataRow[i]["operatetime"]),
                            operatecontent = Convert.ToString(DataRow[i]["operatecontent"])
                        };
                        Listoperationrecords.Add(OR);
                    }

                    App.Current.Dispatcher.Invoke((Action)(() =>
                    {
                        操作记录.ItemsSource = null;
                        操作记录.Items.Clear();
                        操作记录.ItemsSource = Listoperationrecords;
                        操作记录.ScrollIntoView(操作记录.Items[操作记录.Items.Count - 1]);
                    }));
                    connection.Close();
                }
            }
            catch (Exception ex)
            {
                NLOGex(ex);
            }
        }
        public void 新增老化记录(int 通道号)
        {
            try
            {
                var connectionString = @"Data Source=" + Apppath + "datatable.db";
                var connection = new SQLiteConnection(connectionString);
                connection.Open();
                string SheetName = "workingdata";

                string Batchnumber = CosBobject.产品批号;
                string Model = CosBobject.产品型号;
                string Agingcurrent = CosBobject.一阶段电流;
                string Agingtime = CosBobject.一阶段时间;
                string Agingunit = "";
                string Endtime = CosBobject.结束时间;
                string Begintime = CosBobject.CD测试单元格[通道号 - 1].开始时间;
                string IsCompleted = "未完成";
                string Stoptime = "N/A";

                //for (int i = 0; i < PublicClass.CosBobject.CD测试单元格.Count; i++)
                //{
                //    if (PublicClass.CosBobject.CD测试单元格[i].是否被选中)
                //    {
                Agingunit = 通道号.ToString();
                var query = "insert into " + SheetName + " (Batchnumber,Model,Agingcurrent,Agingtime,Agingunit,Begintime,Endtime,Stoptime,IsCompleted) VALUES ('" + Batchnumber + "','" + Model + "','" + Agingcurrent + "','" + Agingtime + "','" + Agingunit + "','" + Begintime + "','" + Endtime + "','" + Stoptime + "','" + IsCompleted + "')";
                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.ExecuteNonQuery();
                }
                //    }
                //}
            }
            catch (Exception ex)
            {
                NLOGex(ex);
            }
        }
        public void 新增老化记录不分离()
        {
            try
            {
                var connectionString = @"Data Source=" + Apppath + "datatable.db";
                var connection = new SQLiteConnection(connectionString);
                connection.Open();
                string SheetName = "workingdata";

                string 被选中 = "", 首数 = "", 尾数 = "";
                for (int i = 0; i < PublicClass.CosBobject.CD测试单元格.Count; i++)
                {
                    if (PublicClass.CosBobject.CD测试单元格[i].是否被选中)
                    {
                        if (首数 == "")
                        {
                            首数 = PublicClass.CosBobject.CD测试单元格[i].通道号.ToString();
                            尾数 = PublicClass.CosBobject.CD测试单元格[i].通道号.ToString();

                        }
                        else
                        {
                            尾数 = PublicClass.CosBobject.CD测试单元格[i].通道号.ToString();
                        }
                    }
                    else
                    {
                        if (首数 == "")
                        {

                        }
                        else
                        {
                            string 新范围 = 首数 + "-" + 尾数;
                            if (首数 == 尾数)
                            {
                                新范围 = 首数;
                            }

                            if (被选中 == " ")
                            {
                                被选中 = 被选中 + 新范围 + ",";

                                首数 = ""; 尾数 = "";
                            }
                        }
                    }
                    if (i == PublicClass.CosBobject.CD测试单元格.Count - 1)
                    {
                        if (首数 == "")
                        {

                        }
                        else
                        {
                            string 新范围 = 首数 + "-" + 尾数;
                            if (首数 == 尾数)
                            {
                                新范围 = 首数;
                            }
                            被选中 = 被选中 + 新范围;
                            首数 = ""; 尾数 = "";
                            char[] charsToTrim = { ',', ' ' };
                            被选中 = 被选中.Trim(charsToTrim);
                        }
                    }
                }

                string Batchnumber = CosBobject.产品批号;
                string Model = CosBobject.产品型号;
                string Agingcurrent = CosBobject.一阶段电流;
                string Agingtime = CosBobject.一阶段时间;
                string Agingunit = 被选中;
                string Endtime = CosBobject.结束时间;
                string Begintime = DateTime.Now.ToString();
                string IsCompleted = "未完成";
                var query = "insert into " + SheetName + " (Batchnumber,Model,Agingcurrent,Agingtime,Agingunit,Begintime,Endtime,IsCompleted) VALUES ('" + Batchnumber + "','" + Model + "','" + Agingcurrent + "','" + Agingtime + "','" + Agingunit + "','" + Begintime + "','" + Endtime + "','" + IsCompleted + "')";
                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.ExecuteNonQuery();
                }
                query = "select * from " + SheetName + " where IsCompleted='未完成' ";
                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    DataTable dt = new DataTable();
                    SQLiteDataReader SQLitereader = command.ExecuteReader();
                    dt.Load(SQLitereader);

                    DataRow[] DataRow = dt.Select();
                    Listworkingdata.Clear();
                    for (int i = 0; i < DataRow.Count(); i++)
                    {
                        workingdata WD = new workingdata()
                        {
                            batchnumber = Convert.ToString(DataRow[i]["Batchnumber"]),
                            model = Convert.ToString(DataRow[i]["Model"]),
                            agingcurrent = Convert.ToString(DataRow[i]["Agingcurrent"]),
                            agingtime = Convert.ToString(DataRow[i]["Agingtime"]),
                            agingunit = Convert.ToString(DataRow[i]["Agingunit"]),
                            begintime = Convert.ToString(DataRow[i]["Begintime"]),
                            endtime = Convert.ToString(DataRow[i]["Endtime"])
                        };
                        Listworkingdata.Add(WD);
                    }

                    App.Current.Dispatcher.Invoke((Action)(() =>
                    {
                        正在加工数据.ItemsSource = null;
                        正在加工数据.Items.Clear();
                        正在加工数据.ItemsSource = Listworkingdata;
                    }));
                }
                connection.Close();

            }
            catch (Exception ex)
            {
                NLOGex(ex);
            }
        }
        public void 更新老化记录(string 产品批号, string 结束时间, int 通道号, string 停止时间)
        {
            try
            {
                var connectionString = @"Data Source=" + Apppath + "datatable.db";
                var connection = new SQLiteConnection(connectionString);
                connection.Open();
                string SheetName = "workingdata";
                var query = "UPDATE  " + SheetName + "  SET   IsCompleted='已完成', Stoptime='" + 停止时间 + "'   where Batchnumber='" + 产品批号 + "'  and  IsCompleted='未完成' and  Agingunit='" + 通道号 + "' and  Endtime='" + 结束时间 + "'";
                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    command.ExecuteNonQuery();
                }

            }
            catch (Exception ex)
            {
                PublicClass.NLOGex(ex);
            }
        }
        public void 更新老化记录显示()
        {
            try
            {
                var connectionString = @"Data Source=" + Apppath + "datatable.db";
                var connection = new SQLiteConnection(connectionString);
                connection.Open();
                string SheetName = "workingdata";

                var query = "select * from " + SheetName + " where IsCompleted='未完成'";
                using (SQLiteCommand command = new SQLiteCommand(query, connection))
                {
                    DataTable dt = new DataTable();
                    SQLiteDataReader SQLitereader = command.ExecuteReader();
                    dt.Load(SQLitereader);
                    DataRow[] DataRow = dt.Select();
                    Listworkingdata.Clear();
                    for (int i = 0; i < DataRow.Count(); i++)
                    {
                        workingdata WD = new workingdata()
                        {
                            batchnumber = Convert.ToString(DataRow[i]["Batchnumber"]),
                            model = Convert.ToString(DataRow[i]["Model"]),
                            agingcurrent = Convert.ToString(DataRow[i]["Agingcurrent"]),
                            agingtime = Convert.ToString(DataRow[i]["Agingtime"]),
                            agingunit = Convert.ToString(DataRow[i]["Agingunit"]),
                            begintime = Convert.ToString(DataRow[i]["Begintime"]),
                            endtime = Convert.ToString(DataRow[i]["Endtime"])
                        };
                        Listworkingdata.Add(WD);
                    }
                    App.Current.Dispatcher.Invoke((Action)(() =>
                    {
                        正在加工数据.ItemsSource = null;
                        正在加工数据.Items.Clear();
                        正在加工数据.ItemsSource = Listworkingdata;
                    }));
                    connection.Close();
                }
            }
            catch (Exception ex)
            {
                PublicClass.NLOGex(ex);
            }
        }
        private void 手动强制结束(object sender, RoutedEventArgs e)
        {        //手动强制结束被选中的通道不分状态
            try
            {
                if (手动结束自锁 == false)
                {
                    手动结束自锁 = true;

                    string STR产品批号 = "", STR结束时间 = "";
                    Sendcmdadding = true;
                    bool 错误检测 = true;
                    //先检测
                    for (int i = 0; i < CosBobject.CD测试单元格.Count; i++)
                    {
                        if (CosBobject.CD测试单元格[i].是否被选中)
                        {
                            if (CosBobject.CD测试单元格[i].通道状态 == 4)
                            {
                                MessageBox.Show("所选通道通信故障！");
                                错误检测 = false;
                                break;
                            }
                            if (CosBobject.CD测试单元格[i].产品批号 != "" & CosBobject.CD测试单元格[i].产品批号 != "N/A")
                            {
                                //MessageBox.Show("所选通道没有产品批号！");
                                //break;
                            }
                            else
                            {
                                if (STR产品批号 == "")//第一次赋值产品批号
                                {
                                    STR产品批号 = CosBobject.CD测试单元格[i].产品批号;
                                }
                                else
                                {
                                    if (STR产品批号 != CosBobject.CD测试单元格[i].产品批号)
                                    {
                                        MessageBox.Show("所选通道通产品批号不同！");
                                        错误检测 = false;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (错误检测)
                    {
                        for (int i = 0; i < CosBobject.CD测试单元格.Count; i++)
                        {
                            if (CosBobject.CD测试单元格[i].是否被选中)
                            {

                                STR产品批号 = CosBobject.CD测试单元格[i].产品批号;
                                STR结束时间 = CosBobject.CD测试单元格[i].结束时间;
                                //第一步还原通道参数
                                CosBobject.CD测试单元格[i].物料编码 = "N/A";
                                CosBobject.CD测试单元格[i].产品批号 = "N/A";
                                CosBobject.CD测试单元格[i].开始时间 = "N/A";
                                CosBobject.CD测试单元格[i].结束时间 = "手动中止";
                                //第二步发命令给下位机停止老化

                                结束流程(PublicClass.CosBobject.CD测试单元格[i].通道号);
                                添加操作记录(CosBobject.CD测试单元格[i].通道号, "手动中止");
                                //第三步消除正在老化列表中的记录
                                if (STR产品批号 != "" & STR结束时间 != "")
                                {
                                    string stoptime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                                    更新老化记录(STR产品批号, STR结束时间, i + 1, stoptime);
                                    更新老化记录显示();
                                    //解除选中
                                    CosBobject.CD测试单元格[i].是否被选中 = false;
                                }
                            }
                        }
                    }
                    Sendcmdadding = false;
                    手动结束自锁 = false;
                }
            }
            catch (Exception ex)
            {
                NLOGex(ex);
                Sendcmdadding = false;
                手动结束自锁 = false;
            }
        }
        private void TextLostFocus(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.TextBox TextBox_Text = (System.Windows.Controls.TextBox)sender;

            //bool result = DCBL.ContainsKey(TextBox_Text.Name);
            if (TextBox_Text.Name == "通道起" || TextBox_Text.Name == "通道止")
            {
                if (TextBox_Text.Text == "")
                {
                    string strShow;
                    strShow = "参数不能为空!";
                    MessageBox.Show(strShow);
                    TextBox_Text.Text = "0";
                    return;
                }
                else if (Convert.ToDouble(TextBox_Text.Text) > 18 || Convert.ToDouble(TextBox_Text.Text) < 0)
                {
                    string strShow;
                    strShow = TextBox_Text.Name + "超限!";
                    MessageBox.Show(strShow);
                    TextBox_Text.Text = "0";
                    return;
                }
            }
        }
        private bool 老化参数完整()
        {
            try
            {
                bool TF = false; bool 单元格被选中 = false;

                if (老化模式.SelectedIndex == -1)
                {
                    MessageBox.Show("请选择老化模式");
                    return false;
                }
                else
                {
                    if (CosBobject.单元报警温度 == "" || CosBobject.水冷报警温度 == "" || CosBobject.脉冲老化电流 == "" || CosBobject.脉冲老化时间 == "")
                    {
                        MessageBox.Show("老化参数不全");
                        return false;
                    }

                    //if (CosBobject.频率 == "" || CosBobject.占空比 == "")
                    //{
                    //    MessageBox.Show("脉冲参数不全");
                    //    return false;
                    //}
                }
                if (CosBobject.通道起 > CosBobject.通道止)
                {
                    string strShow;
                    strShow = "通道开始大于通道结束！";
                    MessageBox.Show(strShow);
                    CosBobject.通道起 = 0; CosBobject.通道止 = 0;
                    return TF;
                }
                if (CosBobject.通道起 > 0 & CosBobject.通道止 > 0)
                {
                    for (int i = 0; i < CosBobject.CD测试单元格.Count; i++)
                    {
                        CosBobject.CD测试单元格[i].是否被选中 = false;
                    }
                    for (int i = CosBobject.通道起; i <= CosBobject.通道止; i++)
                    {
                        CosBobject.CD测试单元格[i - 1].是否被选中 = true;
                    }
                    单元格被选中 = true;
                }
                else//在没有选连续的情况下起作用通道1=通道1
                {
                    for (int i = 0; i < PublicClass.CosBobject.CD测试单元格.Count; i++)
                    {
                        if (PublicClass.CosBobject.CD测试单元格[i].是否被选中)
                        {
                            单元格被选中 = true;
                            break;
                        }
                    }
                }

                if (单元格被选中)
                {
                    bool TF01 = (PublicClass.CosBobject.物料编码 == null || PublicClass.CosBobject.物料编码 == "" || PublicClass.CosBobject.物料编码 == "N/A") ? true : false;
                    bool TF02 = (PublicClass.CosBobject.一阶段电流 == null || PublicClass.CosBobject.一阶段电流 == "" || PublicClass.CosBobject.一阶段电流 == "0") ? true : false;
                    bool TF03 = (PublicClass.CosBobject.一阶段时间 == null || PublicClass.CosBobject.一阶段时间 == "" || PublicClass.CosBobject.一阶段时间 == "0") ? true : false;
                    bool TF04 = (PublicClass.CosBobject.产品批号 == null || PublicClass.CosBobject.产品批号 == "" || PublicClass.CosBobject.产品批号 == "N/A") ? true : false;
                    if (TF01 || TF02 || TF03 || TF04)
                    {
                        MessageBox.Show("缺少老化参数");
                        return false;
                    }
                }
                else
                {
                    MessageBox.Show("未选择测试通道");
                    return false;
                }
                TF = true;
                return TF;
            }
            catch (Exception ex)
            {
                NLOGex(ex);
                return false;
            }
        }
        #region "禁止粘贴和右键"
        private void CommandBinding_CanExecute(Object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
            e.Handled = true;
        }
        #endregion
        private void 单元报警温度_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
        private void 设置脉冲(int 通道号)
        {

            //定义变量           
            List<byte> PackedData = new List<byte>();
            byte[] buffer;
            //设置参数
            PackedData.Add(0x31);
            PackedData.Add(Convert.ToByte(通道号));
            PackedData.Add(0x09);
            byte Hbyte = Convert.ToByte(Math.Floor(Convert.ToDouble(PublicClass.CosBobject.频率) * 10 / 255));
            byte Lbyte = Convert.ToByte(Math.Floor(Convert.ToDouble(PublicClass.CosBobject.频率) * 10 % 255));
            PackedData.Add(Hbyte);
            PackedData.Add(Lbyte);
            Hbyte = Convert.ToByte(Math.Floor(Convert.ToDouble(PublicClass.CosBobject.占空比) * 10 / 255));
            Lbyte = Convert.ToByte(Math.Floor(Convert.ToDouble(PublicClass.CosBobject.占空比) * 10 % 255));
            PackedData.Add(Hbyte);
            PackedData.Add(Lbyte);
            buffer = PublicClass.CS校验和(PackedData.ToArray());
            commandList.AddLast(buffer);
        }
        private void 设置参数(int 通道号)
        {

            //定义变量           
            List<byte> PackedData = new List<byte>();
            byte[] buffer;
            //设置参数
            PackedData.Add(0x31);
            PackedData.Add(Convert.ToByte(通道号));
            PackedData.Add(0x01);
            
            byte Hbyte = Convert.ToByte(Math.Floor(Convert.ToDouble(PublicClass.CosBobject.一阶段电流) * 10 / 255));
            byte Lbyte = Convert.ToByte(Math.Floor(Convert.ToDouble(PublicClass.CosBobject.一阶段电流) * 10 % 255));
            PackedData.Add(Hbyte);
            PackedData.Add(Lbyte);
            byte Temp = Convert.ToByte(Convert.ToInt32(PublicClass.CosBobject.单元报警温度));
            PackedData.Add(Temp);
            buffer = PublicClass.CS校验和(PackedData.ToArray());
            commandList.AddLast(buffer);
        }



        private void 结束流程(int 通道号)
        {
            //定义变量           
            List<byte> PackedData = new List<byte>();
            byte[] buffer;

            //确认选通
            PackedData.Add(0x31);
            PackedData.Add(Convert.ToByte(通道号));
            PackedData.Add(0x02);
            buffer = PublicClass.CS校验和(PackedData.ToArray());
            commandList.AddLast(buffer);
        }
        private void 监控数据(int 通道号)
        {
            //定义变量           
            List<byte> PackedData = new List<byte>();
            byte[] buffer;

            //查询
            PackedData.Add(0x31);
            PackedData.Add(Convert.ToByte(通道号));
            PackedData.Add(0x03);
            buffer = PublicClass.CS校验和(PackedData.ToArray());
            QueryList.AddLast(buffer);
        }
        private void 设置ID(int 通道号)
        {
            //定义变量           
            List<byte> PackedData = new List<byte>();
            byte[] buffer;

            //查询
            PackedData.Add(0x31);
            PackedData.Add(0x00);//ID
            PackedData.Add(0x0B);//功能
            PackedData.Add(Convert.ToByte(通道号));//通道号
            buffer = PublicClass.CS校验和(PackedData.ToArray());
            commandList.AddLast(buffer);
        }
        private void 电磁锁(int 通道号, string 操作)
        {
            //定义变量           
            List<byte> PackedData = new List<byte>();
            byte[] buffer; int 操作号 = 0;
            if (操作 == "吸合")
            {
                操作号 = 1;
            }
            else if (操作 == "打开")
            {
                操作号 = 2;
            }
            //查询
            PackedData.Add(0x31);
            PackedData.Add(Convert.ToByte(通道号));//ID
            PackedData.Add(0x04);//功能
            PackedData.Add(Convert.ToByte(操作号));//通道号
            buffer = PublicClass.CS校验和(PackedData.ToArray());
            commandList.AddLast(buffer);
        }
        private void 查询传感器(int 通道号)
        {
            //定义变量           
            List<byte> PackedData = new List<byte>();
            byte[] buffer;

            //查询
            PackedData.Add(0x31);
            PackedData.Add(Convert.ToByte(通道号));//ID
            PackedData.Add(0x05);//功能
            buffer = PublicClass.CS校验和(PackedData.ToArray());
            commandList.AddLast(buffer);
        }
        private void 设置气缸状态(int 通道号, string 操作)
        {
            //定义变量           
            List<byte> PackedData = new List<byte>();
            byte[] buffer; int 操作号 = 2;
            if (操作 == "打开")
            {
                操作号 = 1;
            }
            else if (操作 == "关闭")
            {
                操作号 = 0;
            }
            //查询
            PackedData.Add(0x31);
            PackedData.Add(Convert.ToByte(通道号));//ID
            PackedData.Add(0x0A);//功能
            PackedData.Add(Convert.ToByte(操作号)); PackedData.Add(Convert.ToByte(操作号)); PackedData.Add(Convert.ToByte(操作号));
            PackedData.Add(Convert.ToByte(操作号)); PackedData.Add(Convert.ToByte(操作号)); PackedData.Add(Convert.ToByte(操作号));//六个气缸状态
            buffer = PublicClass.CS校验和(PackedData.ToArray());
            commandList.AddLast(buffer);
        }

        public void GetCurrentStage1()
        { 
            status= myIniFile.ReadString("Setting", "临时阶梯加电状态", 99);
            if (status == "false")
            {
                string currentStage = myIniFile.ReadString("Setting", "阶段电流1", 99);
                currentStage1 = Convert.ToDouble(currentStage);
            }
            else
            {
                string currentStage = myIniFile.ReadString("Setting", "临时阶段电流1", 99);
                currentStage1 = Convert.ToDouble(currentStage);
            }
        }
        public void 设置加电状态(int 通道号)
        {
            List<byte> PackedData = new List<byte>();
            byte[] buffer;

            PackedData.Add(0x31);
            PackedData.Add(Convert.ToByte(通道号));//ID
            PackedData.Add(0x0C);//功能

            PackedData.Add(0x00);
            PackedData.Add(0x00);
            PackedData.Add(0x00);
            PackedData.Add(0x00);

            PackedData.Add(0x01);
            PackedData.Add(0x00);
            PackedData.Add(0x00);
            PackedData.Add(0x00);

            string state = myIniFile.ReadString("Setting", "临时阶梯加电状态", 99);

            if (state == "false")
            {
                string 有效阶段数 = myIniFile.ReadString("Setting", "有效阶段数", 99);              
                UInt16 num = (UInt16)Convert.ToDouble(有效阶段数);
                byte Hbyte = (byte)(num >> 8);
                byte Lbyte = (byte)(num & 0xff);
                PackedData.Add(Lbyte);
                PackedData.Add(Hbyte);              
                PackedData.Add(0x00);
                PackedData.Add(0x00);

                string 阶段电流1 = myIniFile.ReadString("Setting", "阶段电流1", 99);
                UInt16 a1 = (UInt16)(Convert.ToDouble(阶段电流1)*1000);
                byte Hbyte1 = (byte)(a1 >> 8);
                byte Lbyte1 = (byte)(a1&0xff);
                PackedData.Add(Lbyte1);
                PackedData.Add(Hbyte1);
                string 阶段电流2 = myIniFile.ReadString("Setting", "阶段电流2", 99);
                UInt16 a2 = (UInt16)(Convert.ToDouble(阶段电流2) * 1000);
                byte Hbyte2 = (byte)(a2 >> 8);
                byte Lbyte2 = (byte)(a2 & 0xff);
                PackedData.Add(Lbyte2);
                PackedData.Add(Hbyte2);
                string 阶段电流3 = myIniFile.ReadString("Setting", "阶段电流3", 99);
                UInt16 a3 = (UInt16)(Convert.ToDouble(阶段电流3) * 1000);
                byte Hbyte3 = (byte)(a3 >> 8);
                byte Lbyte3 = (byte)(a3 & 0xff);
                PackedData.Add(Lbyte3);
                PackedData.Add(Hbyte3);
                string 阶段电流4 = myIniFile.ReadString("Setting", "阶段电流4", 99);
                UInt16 a4 = (UInt16)(Convert.ToDouble(阶段电流4) * 1000);
                byte Hbyte4 = (byte)(a4 >> 8);
                byte Lbyte4 = (byte)(a4 & 0xff);
                PackedData.Add(Lbyte4);
                PackedData.Add(Hbyte4);
                string 阶段电流5 = myIniFile.ReadString("Setting", "阶段电流5", 99);
                UInt16 a5 = (UInt16)(Convert.ToDouble(阶段电流5) * 1000);
                byte Hbyte5 = (byte)(a5 >> 8);
                byte Lbyte5 = (byte)(a5 & 0xff);
                PackedData.Add(Lbyte5);
                PackedData.Add(Hbyte5);
                string 阶段电流6 = myIniFile.ReadString("Setting", "阶段电流6", 99);
                UInt16 a6 = (UInt16)(Convert.ToDouble(阶段电流6) * 1000);
                byte Hbyte6 = (byte)(a6 >> 8);
                byte Lbyte6 = (byte)(a6 & 0xff);
                PackedData.Add(Lbyte6);
                PackedData.Add(Hbyte6);
                string 阶段电流7 = myIniFile.ReadString("Setting", "阶段电流7", 99);
                UInt16 a7 = (UInt16)(Convert.ToDouble(阶段电流7) * 1000);
                byte Hbyte7 = (byte)(a7 >> 8);
                byte Lbyte7 = (byte)(a7 & 0xff);
                PackedData.Add(Lbyte7);
                PackedData.Add(Hbyte7);
                string 阶段电流8 = myIniFile.ReadString("Setting", "阶段电流8", 99);
                UInt16 a8 = (UInt16)(Convert.ToDouble(阶段电流8) * 1000);
                byte Hbyte8 = (byte)(a8 >> 8);
                byte Lbyte8 = (byte)(a8 & 0xff);
                PackedData.Add(Lbyte8);
                PackedData.Add(Hbyte8);
                string 阶段电流9 = myIniFile.ReadString("Setting", "阶段电流9", 99);
                UInt16 a9 = (UInt16)(Convert.ToDouble(阶段电流9) * 1000);
                byte Hbyte9 = (byte)(a9 >> 8);
                byte Lbyte9 = (byte)(a9 & 0xff);
                PackedData.Add(Lbyte9);
                PackedData.Add(Hbyte9);
                string 阶段电流10 = myIniFile.ReadString("Setting", "阶段电流10", 99);
                UInt16 a10 = (UInt16)(Convert.ToDouble(阶段电流10) * 1000);
                byte Hbyte10 = (byte)(a10 >> 8);
                byte Lbyte10 = (byte)(a10 & 0xff);
                PackedData.Add(Lbyte10);
                PackedData.Add(Hbyte10);

                string 阶段停留时长1 = myIniFile.ReadString("Setting", "阶段停留时长1", 99);
                UInt16 t1 = (UInt16)Convert.ToDouble(阶段停留时长1);
                byte HbyteT1 = (byte)(t1 >> 8);
                byte LbyteT1 = (byte)(t1 & 0xff);
                PackedData.Add(LbyteT1);
                PackedData.Add(HbyteT1);
                string 阶段停留时长2 = myIniFile.ReadString("Setting", "阶段停留时长2", 99);
                UInt16 t2 = (UInt16)Convert.ToDouble(阶段停留时长2);
                byte HbyteT2 = (byte)(t2 >> 8);
                byte LbyteT2 = (byte)(t2 & 0xff);
                PackedData.Add(LbyteT2);
                PackedData.Add(HbyteT2);
                string 阶段停留时长3 = myIniFile.ReadString("Setting", "阶段停留时长3", 99);
                UInt16 t3 = (UInt16)Convert.ToDouble(阶段停留时长3);
                byte HbyteT3 = (byte)(t3 >> 8);
                byte LbyteT3 = (byte)(t3 & 0xff);
                PackedData.Add(LbyteT3);
                PackedData.Add(HbyteT3);
                string 阶段停留时长4 = myIniFile.ReadString("Setting", "阶段停留时长4", 99);
                UInt16 t4 = (UInt16)Convert.ToDouble(阶段停留时长4);
                byte HbyteT4 = (byte)(t4 >> 8);
                byte LbyteT4 = (byte)(t4 & 0xff);
                PackedData.Add(LbyteT4);
                PackedData.Add(HbyteT4);
                string 阶段停留时长5 = myIniFile.ReadString("Setting", "阶段停留时长5", 99);
                UInt16 t5 = (UInt16)Convert.ToDouble(阶段停留时长5);
                byte HbyteT5 = (byte)(t5 >> 8);
                byte LbyteT5 = (byte)(t5 & 0xff);
                PackedData.Add(LbyteT5);
                PackedData.Add(HbyteT5);
                string 阶段停留时长6 = myIniFile.ReadString("Setting", "阶段停留时长6", 99);
                UInt16 t6 = (UInt16)Convert.ToDouble(阶段停留时长6);
                byte HbyteT6 = (byte)(t6 >> 8);
                byte LbyteT6 = (byte)(t6 & 0xff);
                PackedData.Add(LbyteT6);
                PackedData.Add(HbyteT6);
                string 阶段停留时长7 = myIniFile.ReadString("Setting", "阶段停留时长7", 99);
                UInt16 t7 = (UInt16)Convert.ToDouble(阶段停留时长7);
                byte HbyteT7 = (byte)(t7 >> 8);
                byte LbyteT7 = (byte)(t7 & 0xff);
                PackedData.Add(LbyteT7);
                PackedData.Add(HbyteT7);
                string 阶段停留时长8 = myIniFile.ReadString("Setting", "阶段停留时长8", 99);
                UInt16 t8 = (UInt16)Convert.ToDouble(阶段停留时长8);
                byte HbyteT8 = (byte)(t8 >> 8);
                byte LbyteT8 = (byte)(t8 & 0xff);
                PackedData.Add(LbyteT8);
                PackedData.Add(HbyteT8);
                string 阶段停留时长9 = myIniFile.ReadString("Setting", "阶段停留时长9", 99);
                UInt16 t9 = (UInt16)Convert.ToDouble(阶段停留时长9);
                byte HbyteT9 = (byte)(t9 >> 8);
                byte LbyteT9 = (byte)(t9 & 0xff);
                PackedData.Add(LbyteT9);
                PackedData.Add(HbyteT9);
                string 阶段停留时长10 = myIniFile.ReadString("Setting", "阶段停留时长10", 99);
                UInt16 t10 = (UInt16)Convert.ToDouble(阶段停留时长10);
                byte HbyteT10 = (byte)(t10 >> 8);
                byte LbyteT10 = (byte)(t10 & 0xff);
                PackedData.Add(LbyteT10);
                PackedData.Add(HbyteT10);

            }
            else
            {
                string 临时有效阶段数 = myIniFile.ReadString("Setting", "临时有效阶段数", 99);
                UInt16 num = (UInt16)Convert.ToDouble(临时有效阶段数);
                byte Hbyte = (byte)(num >> 8);
                byte Lbyte = (byte)(num & 0xff);
                PackedData.Add(Lbyte);
                PackedData.Add(Hbyte);
                PackedData.Add(0x00);
                PackedData.Add(0x00);

                string 临时阶段电流1 = myIniFile.ReadString("Setting", "临时阶段电流1", 99);
                UInt16 a1 = (UInt16)(Convert.ToDouble(临时阶段电流1) * 1000);
                byte Hbyte1 = (byte)(a1 >> 8);
                byte Lbyte1 = (byte)(a1 & 0xff);
                PackedData.Add(Lbyte1);
                PackedData.Add(Hbyte1);
                string 临时阶段电流2 = myIniFile.ReadString("Setting", "临时阶段电流2", 99);
                UInt16 a2 = (UInt16)(Convert.ToDouble(临时阶段电流2) * 1000);
                byte Hbyte2 = (byte)(a2 >> 8);
                byte Lbyte2 = (byte)(a2 & 0xff);
                PackedData.Add(Lbyte2);
                PackedData.Add(Hbyte2);
                string 临时阶段电流3 = myIniFile.ReadString("Setting", "临时阶段电流3", 99);
                UInt16 a3 = (UInt16)(Convert.ToDouble(临时阶段电流3) * 1000);
                byte Hbyte3 = (byte)(a3 >> 8);
                byte Lbyte3 = (byte)(a3 & 0xff);
                PackedData.Add(Lbyte3);
                PackedData.Add(Hbyte3);
                string 临时阶段电流4 = myIniFile.ReadString("Setting", "临时阶段电流4", 99);
                UInt16 a4 = (UInt16)(Convert.ToDouble(临时阶段电流4) * 1000);
                byte Hbyte4 = (byte)(a4 >> 8);
                byte Lbyte4 = (byte)(a4 & 0xff);
                PackedData.Add(Lbyte4);
                PackedData.Add(Hbyte4);
                string 临时阶段电流5 = myIniFile.ReadString("Setting", "临时阶段电流5", 99);
                UInt16 a5 = (UInt16)(Convert.ToDouble(临时阶段电流5) * 1000);
                byte Hbyte5 = (byte)(a5 >> 8);
                byte Lbyte5 = (byte)(a5 & 0xff);
                PackedData.Add(Lbyte5);
                PackedData.Add(Hbyte5);
                string 临时阶段电流6 = myIniFile.ReadString("Setting", "临时阶段电流6", 99);
                UInt16 a6 = (UInt16)(Convert.ToDouble(临时阶段电流6) * 1000);
                byte Hbyte6 = (byte)(a6 >> 8);
                byte Lbyte6 = (byte)(a6 & 0xff);
                PackedData.Add(Lbyte6);
                PackedData.Add(Hbyte6);
                string 临时阶段电流7 = myIniFile.ReadString("Setting", "临时阶段电流7", 99);
                UInt16 a7 = (UInt16)(Convert.ToDouble(临时阶段电流7) * 1000);
                byte Hbyte7 = (byte)(a7 >> 8);
                byte Lbyte7 = (byte)(a7 & 0xff);
                PackedData.Add(Lbyte7);
                PackedData.Add(Hbyte7);
                string 临时阶段电流8 = myIniFile.ReadString("Setting", "临时阶段电流8", 99);
                UInt16 a8 = (UInt16)(Convert.ToDouble(临时阶段电流8) * 1000);
                byte Hbyte8 = (byte)(a8 >> 8);
                byte Lbyte8 = (byte)(a8 & 0xff);
                PackedData.Add(Lbyte8);
                PackedData.Add(Hbyte8);
                string 临时阶段电流9 = myIniFile.ReadString("Setting", "临时阶段电流9", 99);
                UInt16 a9 = (UInt16)(Convert.ToDouble(临时阶段电流9) * 1000);
                byte Hbyte9 = (byte)(a9 >> 8);
                byte Lbyte9 = (byte)(a9 & 0xff);
                PackedData.Add(Lbyte9);
                PackedData.Add(Hbyte9);
                string 临时阶段电流10 = myIniFile.ReadString("Setting", "临时阶段电流10", 99);
                UInt16 a10 = (UInt16)(Convert.ToDouble(临时阶段电流10) * 1000);
                byte Hbyte10 = (byte)(a10 >> 8);
                byte Lbyte10 = (byte)(a10 & 0xff);
                PackedData.Add(Lbyte10);
                PackedData.Add(Hbyte10);

                string 临时阶段停留时长1 = myIniFile.ReadString("Setting", "临时阶段停留时长1", 99);
                UInt16 t1 = (UInt16)Convert.ToDouble(临时阶段停留时长1);
                byte HbyteT1 = (byte)(t1 >> 8);
                byte LbyteT1 = (byte)(t1 & 0xff);
                PackedData.Add(LbyteT1);
                PackedData.Add(HbyteT1);
                string 临时阶段停留时长2 = myIniFile.ReadString("Setting", "临时阶段停留时长2", 99);
                UInt16 t2 = (UInt16)Convert.ToDouble(临时阶段停留时长2);
                byte HbyteT2 = (byte)(t2 >> 8);
                byte LbyteT2 = (byte)(t2 & 0xff);
                PackedData.Add(LbyteT2);
                PackedData.Add(HbyteT2);
                string 临时阶段停留时长3 = myIniFile.ReadString("Setting", "临时阶段停留时长3", 99);
                UInt16 t3 = (UInt16)Convert.ToDouble(临时阶段停留时长3);
                byte HbyteT3 = (byte)(t3 >> 8);
                byte LbyteT3 = (byte)(t3 & 0xff);
                PackedData.Add(LbyteT3);
                PackedData.Add(HbyteT3);
                string 临时阶段停留时长4 = myIniFile.ReadString("Setting", "临时阶段停留时长4", 99);
                UInt16 t4 = (UInt16)Convert.ToDouble(临时阶段停留时长4);
                byte HbyteT4 = (byte)(t4 >> 8);
                byte LbyteT4 = (byte)(t4 & 0xff);
                PackedData.Add(LbyteT4);
                PackedData.Add(HbyteT4);
                string 临时阶段停留时长5 = myIniFile.ReadString("Setting", "临时阶段停留时长5", 99);
                UInt16 t5 = (UInt16)Convert.ToDouble(临时阶段停留时长5);
                byte HbyteT5 = (byte)(t5 >> 8);
                byte LbyteT5 = (byte)(t5 & 0xff);
                PackedData.Add(LbyteT5);
                PackedData.Add(HbyteT5);
                string 临时阶段停留时长6 = myIniFile.ReadString("Setting", "临时阶段停留时长6", 99);
                UInt16 t6 = (UInt16)Convert.ToDouble(临时阶段停留时长6);
                byte HbyteT6 = (byte)(t6 >> 8);
                byte LbyteT6 = (byte)(t6 & 0xff);
                PackedData.Add(LbyteT6);
                PackedData.Add(HbyteT6);
                string 临时阶段停留时长7 = myIniFile.ReadString("Setting", "临时阶段停留时长7", 99);
                UInt16 t7 = (UInt16)Convert.ToDouble(临时阶段停留时长7);
                byte HbyteT7 = (byte)(t7 >> 8);
                byte LbyteT7 = (byte)(t7 & 0xff);
                PackedData.Add(LbyteT7);
                PackedData.Add(HbyteT7);
                string 临时阶段停留时长8 = myIniFile.ReadString("Setting", "临时阶段停留时长8", 99);
                UInt16 t8 = (UInt16)Convert.ToDouble(临时阶段停留时长8);
                byte HbyteT8 = (byte)(t8 >> 8);
                byte LbyteT8 = (byte)(t8 & 0xff);
                PackedData.Add(LbyteT8);
                PackedData.Add(HbyteT8);
                string 临时阶段停留时长9 = myIniFile.ReadString("Setting", "临时阶段停留时长9", 99);
                UInt16 t9 = (UInt16)Convert.ToDouble(临时阶段停留时长9);
                byte HbyteT9 = (byte)(t9 >> 8);
                byte LbyteT9 = (byte)(t9 & 0xff);
                PackedData.Add(LbyteT9);
                PackedData.Add(HbyteT9);
                string 临时阶段停留时长10 = myIniFile.ReadString("Setting", "临时阶段停留时长10", 99);
                UInt16 t10 = (UInt16)Convert.ToDouble(临时阶段停留时长10);
                byte HbyteT10 = (byte)(t10 >> 8);
                byte LbyteT10 = (byte)(t10 & 0xff);
                PackedData.Add(LbyteT10);
                PackedData.Add(HbyteT10);
            }

            PackedData.Add(0x00);
            PackedData.Add(0x00);
            PackedData.Add(0x00);
            PackedData.Add(0x00);

            PackedData.Add(0x00);
            PackedData.Add(0x00);
            PackedData.Add(0x00);
            PackedData.Add(0x00);

            PackedData.Add(0x00);
            PackedData.Add(0x00);
            PackedData.Add(0x00);
            PackedData.Add(0x00);

            buffer = PublicClass.CS校验和(PackedData.ToArray());
            commandList.AddLast(buffer);
        }
            
    }
}
